module.controller('LogManagementController',function($scope, $timeout, $q,$cookies,logService,k8sService) {

  // var ns = $cookies.get("username");
  $scope.logSub={};
  $scope.logSub.serviceName=$scope.logSub.serviceName=="undefined"?$scope.logSub.serviceName:"服务名";
  // console.log($scope.logSub.serviceName);
  $scope.logSub.caseNum=$scope.logSub.serviceName=="undefined"?$scope.logSub.serviceName:"实例数";
  $scope.logSub.startDate=new Date();
  $scope.logSub.endDate=new Date();
  $scope.logOptions = {
        lineWrapping : false,
        autoRefresh:true,
        lineNumbers: true,
        theme:'monokai',
        model: 'yaml',
      readOnly: 'nocursor',
    };
/*$scope.animateClick=function(){
  var flag=jQuery(".dropdown-menu").parent('.dropdown').is(jQuery(".dropdown.Open"));
  console.log("1---"+flag);
    document.getElementsByClassName("dropdown-menu").addEventListener("transitionend",function () {
      var flag=jQuery(".dropdown-menu").parent('.dropdown').is(jQuery(".dropdown.Open"));
      console.log("2---"+flag);
    })
}*/


    $scope.serviceNames = [];
    $scope.$watch('selectedIndex', function(){
        k8sService.getServices(undefined).then(function(result){
            var services = result.data; //加载当前集群tab下的services
            if(services && services.length > 0){
                for(var i = 0; i < services.length; i++){
                    $scope.serviceNames[i] = services[i].app;
                }
                // $scope.logSub.serviceName = $scope.serviceNames[0];
            }
        }, function(err){
            if(!err || ! err.data ){
                err = {
                    data:"Request timeout!"
                }
            }
            alertify.error(err.data);
        });
    });
    $scope.selectService = {
        onSelect: function (serviceName) {
            $scope.pods = [];
          var svc = {
            app:serviceName,
              node:{
                  clusterId:'default'
              }
          };
            k8sService.getpodStatus(svc).then(function (result) {
              var items = result.data.items;
              if(items && items.length > 0){
                  for(var i = 0 ; i < items.length; i++){
                      $scope.pods[i] = items[i].metadata.name;
                  }
                  // $scope.logSub.caseNum = $scope.pods[0];
              }
              // console.log($scope.pods[0]);

            },function (err) {
                console.log(err);
            });
        }
    };
    $scope.selectService.onSelect($scope.logSub.serviceName);

    $scope.logSubmit=function () {

        var startYear = $scope.logSub.startDate.getFullYear();
        var startMonth = $scope.logSub.startDate.getMonth() + 1;
        var startDatesd = $scope.logSub.startDate.getDate();
        var startDates = startYear + '-' + startMonth + '-' + startDatesd;

        var endYear = $scope.logSub.endDate.getFullYear();
        var endMonth = $scope.logSub.endDate.getMonth() + 1;
        var endDatesd = $scope.logSub.endDate.getDate();
        var endDates = endYear + '-' + endMonth + '-' + endDatesd;

        var param = {
            po:$scope.logSub.caseNum,
            st:startDates,
            ed:endDates,
            key:$scope.logSub.keyWord
        };

        logService.search(param).then(function (result) {
            var log='';
           for(var i=0;i<result.data.length;i++){
               log+=result.data[i];
           }
            $scope.logsContent=log;
        },function (err) {
            console.log(err);
        });

    }

    
    /**
     * 放大
     * */
    $scope.enlargeLogContentflag=false;

});